package com.yq.cp.service.basics.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yq.cp.config.MyException;
import com.yq.cp.entity.basics.Department;
import com.yq.cp.entity.basics.OrganizationalStructure;
import com.yq.cp.entity.basics.Position;
import com.yq.cp.entity.permission.User;
import com.yq.cp.mapper.basics.DepartmentMapper;
import com.yq.cp.mapper.basics.OrganizationalStructureMapper;
import com.yq.cp.mapper.basics.PositionMapper;
import com.yq.cp.mapper.permission.RolePermissionMapper;
import com.yq.cp.mapper.permission.UserMapper;
import com.yq.cp.service.basics.PositionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

/**
 * <p>
 * 职位表 服务实现类
 * </p>
 *
 * @author shenyuqing
 * @since 2024-08-27
 */
@Service
public class PositionServiceImpl extends ServiceImpl<PositionMapper, Position> implements PositionService {
    @Resource
    PositionMapper positionMapper;
    @Resource
    RolePermissionMapper rolePermissionMapper;
    @Resource
    UserMapper userMapper;
    @Resource
    DepartmentMapper departmentMapper;
    @Resource
    OrganizationalStructureMapper organizationalStructureMapper;

    @Override
    public User listUserToPosition(User vo) {
        User user = userMapper.selectOne(new QueryWrapper<User>()
                .eq("nickname", vo.getNickname()).eq("phone", vo.getPhone())
                .eq("site_id",vo.getId()));
        if (user==null || Objects.isNull(user.getPositionId()))
            throw new MyException("该人员无职位，请重新输入后再试！！！");
        Position position = positionMapper.selectById(user.getPositionId());
        Department department = departmentMapper.selectById(user.getDepartmentId());
        OrganizationalStructure structure = organizationalStructureMapper.selectById(user.getSiteId());
        OrganizationalStructure structure1 = organizationalStructureMapper.selectById(structure.getParentId());
        OrganizationalStructure structure2 = organizationalStructureMapper.selectById(structure1.getParentId());
        OrganizationalStructure structure3 = organizationalStructureMapper.selectById(structure2.getParentId());
        OrganizationalStructure structure4 = organizationalStructureMapper.selectById(structure3.getParentId());
        OrganizationalStructure structure5= organizationalStructureMapper.selectById(structure4.getParentId());
        user.setPositionName(position.getName());
        user.setDepartmentName(department.getName());
        user.setSiteName(structure.getName());
        user.setQuId(structure1.getId());
        user.setQuName(structure1.getName());
        user.setShiId(structure2.getId());
        user.setShiName(structure2.getName());
        user.setShengId(structure3.getId());
        user.setShengName(structure3.getName());
        user.setDaquId(structure4.getId());
        user.setDaquName(structure4.getName());
        user.setQudaoshangId(structure5.getId());
        user.setQudaoshangName(structure5.getName());
        return user;
    }
}
